#pragma once

#include <list>
#include <string>
#include <thread>
#include <mutex>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/eventfd.h>
#include <functional>
#include <iostream>

struct DataPacket{
  const uint8_t* data;
  uint32_t len;
};

class UdpClient
{
private:
    /* data */
    std::shared_ptr<std::thread> process_thread_;
    std::string m_ip_;
    uint16_t m_port_;
    int udp_socket_;
    std::list<DataPacket> send_data_buf_;
    std::mutex tx_buf_mutex_;
    int m_event_socket_;
    bool m_socket_is_open_;
    bool m_process_thread_start_;
    struct sockaddr_in remote;

    std::function<void(uint8_t* data, int32_t len)> callback_;

    bool ProcessLoop();
public:
    UdpClient(/* args */);
    ~UdpClient();

    bool Init(std::string ip, uint16_t port);

    bool Start();

    bool Stop();

    bool SendData(const uint8_t *data, int32_t len);
    
    void RegCallback( std::function<void(uint8_t* data, int32_t len)> call){
        callback_ = call;
    };
};


